<?php
/**
 * Class that operate on table '${table_name}'. Database Mysql.
 *
 * @version    $Id$ 
 * @author: ${author} 
 * @date: ${date} 
 */
class ${class_name_dao}DAO implements ${class_name_idao}DAO
{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return ${class_name_dao} 
	 */
	public function load($id) {
		$sql = 'SELECT * FROM ${table_name} WHERE ${pk} = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set${pk_number}($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll() {
		$sql = 'SELECT * FROM ${table_name}';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn) {
		$sql = 'SELECT * FROM ${table_name} ORDER BY ' . $orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Delete record from table
	 * @param ${variable_name} primary key
	 */
	public function delete($${pk}) {
		$sql = 'DELETE FROM ${table_name} WHERE ${pk} = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set${pk_number}($${pk});
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
	 * Insert record to table
	 *
	 * @param ${class_name_dao} ${variable_name}
	 */
	public function insert($${variable_name}) {
		$sql = 'INSERT INTO ${table_name} (${insert_fields}) VALUES (${question_marks})';
		$sqlQuery = new SqlQuery($sql);
		${parameter_setter}
		$id = $this->executeInsert($sqlQuery);	
		$${variable_name}->${pk_php} = $id;
		return $id;
	}
	
	/**
	 * Update record in table
	 *
	 * @param ${class_name_dao} ${variable_name}
	 */
	public function update($${variable_name}) {
		$sql = 'UPDATE ${table_name} SET ${update_fields} WHERE ${pk} = ?';
		$sqlQuery = new SqlQuery($sql);
		${parameter_setter}
		$sqlQuery->set${pk_number}($${variable_name}->${pk_php});
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
	 * Delete all rows
	 */
	public function clean() {
		$sql = 'DELETE FROM ${table_name}';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

${queryByFieldFunctions}
${deleteByFieldFunctions}
	
	/**
	 * Read row
	 *
	 * @return ${class_name_dao} 
	 */
	protected function readRow($row) {
		$${variable_name} = new ${class_name_domain}();
		${read_row}
		return $${variable_name};
	}
	
	protected function getList($sqlQuery) {
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++) {
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return ${class_name_dao} 
	 */
	protected function getRow($sqlQuery) {
		$tab = QueryExecutor::execute($sqlQuery);
		if (count($tab) == 0) {
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery) {
		return QueryExecutor::execute($sqlQuery);
	}
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery) {
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery) {
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery) {
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>